CREATE TABLE [dbo].[CodeflowInstances](	
	[Id] [uniqueidentifier] DEFAULT NEWSEQUENTIALID() NOT NULL CONSTRAINT PK_CodeflowInstances_Id PRIMARY KEY NONCLUSTERED,
	[CodeflowType] [nvarchar](1024) NOT NULL,
	[FriendlyName] [nvarchar](1024) NOT NULL,
	[Status] int NOT NULL,		
	[TimeToWakeUp] [datetime] NULL,
	[LockedUntilUtc] [datetime] NOT NULL,
	[DispatcherId] [uniqueidentifier],	
	[ActionName] [nvarchar](max) NOT NULL,		
	[ArgumentsJson] [nvarchar](max) NULL,	
	[DispatchAttemptsCount] [int] NOT NULL,
	[StartedAtUtc] [datetime] NOT NULL,			
	[SerializedArguments] [varbinary](max) NULL,	
	[CreatedOnUtc] [datetime] NOT NULL,
	[ModifiedOnUtc] [datetime] NOT NULL)
GO

CREATE TABLE [dbo].[CodeflowActions](	
	[Id] [uniqueidentifier] DEFAULT NEWSEQUENTIALID() NOT NULL CONSTRAINT PK_CodeflowAction_Id PRIMARY KEY NONCLUSTERED,
	[CodeflowId] [uniqueidentifier] NOT NULL,
	[ActionName] [nvarchar](max) NOT NULL,		
	[ArgumentsJson] [nvarchar](max) NULL,	
	[DispatchAttemptsCount] [int] NOT NULL,
	[StartedAtUtc] [datetime] NOT NULL,
	[CompletedAtUtc] [datetime] NULL,
	[Result] int NULL,			
	[ExceptionData] [nvarchar](max) NULL)
GO

CREATE TABLE [dbo].[CodeflowsDispatch](
	[Id]				UNIQUEIDENTIFIER PRIMARY KEY NOT NULL,
	[FirstDispatchOn]	DATETIME NOT NULL DEFAULT GETDATE(),
	[LastDispatch]		DATETIME NOT NULL DEFAULT GETDATE(),
	[IsFailed]			BIT NOT NULL DEFAULT 0	
)
GO

CREATE TABLE [dbo].[CodeflowsConfiguration](
	[Id]	INT PRIMARY KEY NONCLUSTERED IDENTITY NOT NULL,
	[Key]	VARCHAR(256) NOT NULL,
	[Value]	VARCHAR(256) NOT NULL
)

CREATE TABLE [dbo].[CodeflowsLog](
	[Id]		INT PRIMARY KEY CLUSTERED IDENTITY NOT NULL,
	[Type]		VARCHAR(20) NOT NULL,
	[Data]		VARCHAR(MAX) NOT NULL,
	[CreatedOn]	DATETIME NOT NULL DEFAULT GETDATE()
)
GO

CREATE TABLE [dbo].[CodeflowsRuntime](
	[Id]					INT PRIMARY KEY CLUSTERED IDENTITY NOT NULL,
	[DispatchFailureCount]	INT
)
GO